(function (c) {
    c.suggest = function (d, b) {
        function h() {
            if (!e.is(":visible"))return!1;
            var a = e.children("ol").children("li." + b.selectClass);
            a.length || (a = !1);
            return a
        }

        function f() {
            if ($currentResult = h()) {
                var a = $currentResult.children("a");
                g.val(a.text());
                a.attr("data-id") && "" != a.attr("data-id") && g.attr("data-id", a.attr("data-id"));
                b.selectedCurrentClk(a.text());
                e.hide()
            }
        }

        var g = d, e;
        c("#" + b.containerId)[0] || g.closest("div").append('<div class="suggest-wrap"><div id="' + b.containerId + '" class="suggest" style="top:29px;left:8px"><div class="suggest-tip"></div><ol class="autocomplete"></ol></div></div>');
        e = c("#" + b.containerId);
        e.hide();
        var i = null;
        g.live("keyup", function (a) {
            if (/27$|38$|40$/.test(a.keyCode) && e.is(":visible") || /^13$|^9$/.test(a.keyCode) && h())switch (a.preventDefault && a.preventDefault(), a.stopPropagation && a.stopPropagation(), a.cancelBubble = !0, a.returnValue = !1, a.keyCode) {
                case 38:
                    ($currentResult = h()) ? $currentResult.removeClass(b.selectClass).prev().addClass(b.selectClass) : e.children("ol").children("li:last-child").addClass(b.selectClass);
                    break;
                case 40:
                    ($currentResult = h()) ? $currentResult.removeClass(b.selectClass).next().addClass(b.selectClass) :
                        e.children("ol").children("li:first-child").addClass(b.selectClass);
                    break;
                case 13:
                    f();
                    break;
                case 27:
                    e.hide()
            } else {
                var d = c.trim(g.val());
                i && clearTimeout(i);
                e.hide();
                if ("" == d)return!1;
                i = setTimeout(function () {
                        c.ajax({type:"post", dataType:"json", data:c.extend({keywords:d, limit:b.limit, from:"front"}, b.ajaxData), url:b.ajaxUrl, success:function (a) {
                            c(".autocomplete").empty().html("");
                            c(".suggest-tip").html("");
                            if (110 == a.code)c(".suggest-tip").html("无匹配项~"), c("#" + b.containerId).hide(); else if (100 ==
                                a.code) {
                                c("#" + b.containerId).show();
                                var d = a[b.listName].length;
                                b.successClk(a);
                                0 == d ? c(".suggest-tip").html("无匹配项~") : (c(".suggest-tip").html("共 " + d + " 条匹配项"), e.children("ol").children("li:first-child").addClass(b.selectClass), e.children("ol").children("li").mouseover(function () {
                                    e.children("ol").children("li").removeClass(b.selectClass);
                                    c(this).addClass(b.selectClass)
                                }).click(function (a) {
                                    a.preventDefault();
                                    f()
                                }))
                            } else c(".suggest-tip").html("服务器返回异常~")
                        }})
                    },
                    b.delay)
            }
        });
        document.onclick = function (a) {
            var d = c("#" + b.containerId), a = a || window.event, e = a.srcElement || a.target, e = a.srcElement ? a.srcElement : a.target;
            e != d[0] && "LI" != e.tagName && d.hide()
        }
    };
    c.fn.suggest = function (d) {
        var d = c.extend({type:"tag", limit:50, listName:"tagList", ajaxUrl:"", ajaxData:{}, delay:500, selectClass:"hover", containerId:"J_SuggestResult", successClk:function () {
        }, selectedCurrentClk:function () {
        }}, d || {}), b = this;
        this.each(function () {
            new c.suggest(b, d)
        })
    };
    c.searchSuggest = function (d, b) {
        d.suggest({type:"search",
            containerId:b.containerId, listName:"productSearchSuggestVOList", ajaxUrl:GUANGER.path + "ajax/aj_get_search_product_suggest.php", ajaxData:{contentType:"search"}, successClk:function (d) {
                if (0 != d.productSearchSuggestVOList.length) {
                    var f = "";
                    c.map(d.productSearchSuggestVOList, function (b) {
                        f += '<li><a class="ofh">' + b.keywords + "</a></li>"
                    });
                    c("#" + b.containerId).find(".autocomplete:first").append(f)
                }
            }, selectedCurrentClk:function () {
                c("#J_SearchBarSubmit").click()
            }})
    }
})(jQuery);
$(function () {
    $.searchSuggest($("#J_SearchKeyword"), {containerId:"J_SearchSuggestList"})
});
